﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using NHibernate.Criterion;
using Project.Common;
using Project.Model;
using System.Linq;

namespace Web
{
    /// <summary>
    /// 功能：分页列表页面
    ///       (Plantable)
    /// 作者：luky
    /// 日期：2010-12-28
    /// </summary>
    public partial class ShquserManager_List : AdminPage
    {
        //创建查询器
        IList<ICriterion> queryList = new List<ICriterion>();
        // 排序字段
        Order order;

        /// <summary>
        /// 年限
        /// </summary>
        protected string jhyear { get { return base.QueryString("jhyear"); } }
        /// <summary>
        /// 年限
        /// </summary>
        protected string jhname { get { return base.QueryString("jhname"); } }

        /// <summary>
        /// 审核状态
        /// </summary>
        protected string checkqk { get { return base.QueryString("checkqk"); } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<string> yearList = ManagerFactory.ShqUserManagerManager.GetDistinctList<string>(PropertyNameHelper.Resolve<ShqUserManager>(d => d.Jhyear));
                //JhYearDropDownList.DataSource = yearList;
                //JhYearDropDownList.DataBind();
                foreach (string item in yearList)
                {
                    if(!string.IsNullOrEmpty(item))
                        JhYearDropDownList.Items.Add(new ListItem(item.Trim() + "年", item.Trim()));
                }
                JhYearDropDownList.Items.Insert(0,new ListItem("所有", ""));

                IList<Plantable> PlantableList = ManagerFactory.PlantableManager.GetAll().ToList();
                //JhTypeDropDownList.DataSource = ManagerFactory.PlantableManager.GetAll();
                //JhTypeDropDownList.DataValueField = PropertyNameHelper.Resolve<Plantable>(d => d.Id);
                //JhTypeDropDownList.DataTextField = PropertyNameHelper.Resolve<Plantable>(d => d.Jhname);
                foreach (Plantable item in PlantableList)
                {
                    if (!string.IsNullOrEmpty(item.Jhname))
                        JhTypeDropDownList.Items.Add(new ListItem(item.Jhname.Trim(), item.Jhname.Trim()));
                }
                JhTypeDropDownList.DataBind();
                JhTypeDropDownList.Items.Insert(0, new ListItem("所有", ""));

                base.BindEnum2WebControls(CheckQKDropDownList, typeof(EnumCheckQK), true, 0);
                CheckQKDropDownList.Items.Insert(0, new ListItem("全部", ""));

                BindGrid();
            }
        }
        #region 搜索

        protected void searchButton_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void QuerySelect()
        {
            if (JhYearDropDownList.Items.FindByValue(jhyear) != null)
            {
                JhYearDropDownList.ClearSelection();
                JhYearDropDownList.Items.FindByValue(jhyear).Selected = true;
            }
            if (JhTypeDropDownList.Items.FindByValue(jhname) != null)
            {
                JhTypeDropDownList.ClearSelection();
                JhTypeDropDownList.Items.FindByValue(jhname).Selected = true;
            }
            if (CheckQKDropDownList.Items.FindByValue(checkqk) != null)
            {
                CheckQKDropDownList.ClearSelection();
                CheckQKDropDownList.Items.FindByValue(checkqk).Selected = true;
            }

            // 传入条件
            // 添加自定义条件
            if (!string.IsNullOrEmpty(txt_Item_name.Text))
                queryList.Add(Restrictions.Like(PropertyNameHelper.Resolve<ShqUserManager>(d => d.Itemname), txt_Item_name.Text.Trim(), MatchMode.Anywhere));
            if (!string.IsNullOrEmpty(txt_UserName_name.Text))
                queryList.Add(Restrictions.Like(PropertyNameHelper.Resolve<ShqUserManager>(d => d.USERNAME), txt_UserName_name.Text.ToString()));
            if (!string.IsNullOrEmpty(txt_Shbdwname_name.Text))
                queryList.Add(Restrictions.Like(PropertyNameHelper.Resolve<ShqUserManager>(d => d.Company), txt_Shbdwname_name.Text.Trim(), MatchMode.Anywhere));
            if (!string.IsNullOrEmpty(jhyear))
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<ShqUserManager>(d => d.Jhyear), jhyear));
            if (!string.IsNullOrEmpty(jhname))
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<ShqUserManager>(d => d.Jhname), jhname));
            if (!string.IsNullOrEmpty(checkqk) && Validator.IsInt(checkqk))
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<ShqUserManager>(d => d.Checkqk), (double.Parse(checkqk)/10).ToString()));
            // 不要显示 计划名称为空的记录
            queryList.Add(Restrictions.IsNotNull(PropertyNameHelper.Resolve<ShqUserManager>(d => d.Jhname)));
        }

        #endregion

        private void BindGrid()
        {
            this.QuerySelect();
            AspNetPager1.RecordCount = ManagerFactory.ShqUserManagerManager.GetCount(queryList);
            IList<ShqUserManager> list = ManagerFactory.ShqUserManagerManager.GetPageList(AspNetPager1, queryList, order);
            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        #region gridview绑定及命令事件

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            object itemId = e.CommandArgument;
            
            
            if (e.CommandName == "DeleteIt")
            {
                #region 约束检查
                bool IsExistFK = false;
                #endregion
                if (!IsExistFK)
                {
                    if (currentUser.UserType == ((int)EnumUserType.管理员).ToString())
                    {
                        ManagerFactory.ShqUserManagerManager.Delete(int.Parse(itemId.ToString()));
                        // ManagerFactory.ShqryuserManagerManager.Delete(int.Parse(itemId.ToString()));
                        //IQueryable<ShqryuserManager> list = ManagerFactory.ShqryuserManagerManager;
                        IList<ICriterion> queryList1 = new List<ICriterion>();
                        queryList1.Add(Restrictions.Eq(PropertyNameHelper.Resolve<ShqryuserManager>(d => d.Shqid), itemId.ToString()));
                        //ShqryuserManager shq = 
                        IList<ShqryuserManager> shq = ManagerFactory.ShqryuserManagerManager.GetList(queryList1);
                        int id = int.Parse(shq.First().Id.ToString());
                        ManagerFactory.ShqryuserManagerManager.Delete(id);

                        IList<ICriterion> queryList2 = new List<ICriterion>();
                        queryList2.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Indexbasictable>(d => d.shqid), itemId.ToString()));
                        IList<Indexbasictable> shqq = ManagerFactory.IndexbasictableManager.GetList(queryList2);
                        int id1 = int.Parse(shqq.First().Id.ToString());
                        ManagerFactory.IndexbasictableManager.Delete(id1);

                        IList<ICriterion> queryList3 = new List<ICriterion>();
                        queryList2.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Itemtable>(d => d.shqid), itemId.ToString()));
                        IList<Itemtable> shqqq = ManagerFactory.ItemtableManager.GetList(queryList3);
                        int id11 = int.Parse(shqqq.First().Id.ToString());
                        ManagerFactory.ItemtableManager.Delete(id11);

                        BindGrid();
                    }
                    else
                        Alert("权限不足！");
                }
                else
                {
                    ExecJsAjax("alert('该记录已经被使用,不能删除！');", Page);
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //string cssName;
                //if (e.Row.RowState == DataControlRowState.Alternate) cssName = "grid_alt_item";
                //else cssName = "grid_item";
                //e.Row.Attributes.Add("onmouseout", "this.className='" + cssName + "'");
                //e.Row.Attributes.Add("onmouseover", "this.className='" + "gridSelectedItem" + "'");
            }
        }

        #endregion

        #region 分页

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion

        #region 排序

        public string SortD
        {
            get
            {
                if (ViewState["SortD"] == null)
                {
                    switch (GridView1.SortDirection)
                    {
                        case SortDirection.Ascending:
                            ViewState["SortD"] = "ASC";
                            break;
                        case SortDirection.Descending:
                            ViewState["SortD"] = "DESC";
                            break;
                    }
                }
                return Convert.ToString(ViewState["SortD"]);
            }
            set
            {
                ViewState["SortD"] = value;
            }
        }
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortD = (SortD.ToLower() == "asc" ? "DESC" : "ASC");
            order = new Order(e.SortExpression, SortD.ToLower() == "ASC".ToLower());
        }
        protected void GridView1_Sorted(object sender, EventArgs e)
        {
            BindGrid();
        }

        #endregion

        protected void JhYearDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Response.Redirect("List.aspx?jhyear=" + JhYearDropDownList.SelectedValue);
            Response.Redirect(WebHelper.BuildUrl(Request.Url.ToString(), new string[] { "jhyear" }, new string[] { JhYearDropDownList.SelectedValue }));
        }
        protected void JhTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Response.Redirect("List.aspx?jhyear=" + JhYearDropDownList.SelectedValue);
            Response.Redirect(WebHelper.BuildUrl(Request.Url.ToString(), new string[] { "jhyear", "jhname" }, new string[] { JhYearDropDownList.SelectedValue, JhTypeDropDownList.SelectedValue }));
        }
        protected void CheckQKDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(WebHelper.BuildUrl(Request.Url.ToString(), new string[] { "checkqk" }, new string[] { CheckQKDropDownList.SelectedValue}));
        }
    }
}
